import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import api from '../http/api'
import { inject } from 'vue'


export default () => {
	
	const store = useStore()
	const router = useRouter()
	
	const getOpenid = () => {
		function guid() {
			return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
				var r = Math.random() * 16 | 0,
					v = c == 'x' ? r : (r & 0x3 | 0x8);
				return v.toString(16);
			})
		}

		if (!window.localStorage.hasOwnProperty('openid')) {
			let __openid = guid()
			window.localStorage.setItem('openid', guid())
			return __openid
		} else {
			return window.localStorage.getItem('openid')
		}
	}

	const loginByOpenid = async (cb) => {
		const openid = sessionStorage.getItem('openid')
		if (!openid) return
		api.openidLogin(openid).then(res => {
			if (!res.code) {
				if (!res.data) {
					router.replace('/login');
					return
				}

				if (res.data.role == 1) {
					store.commit('doctor/SET_INFO', res.data.accountInfo)
					const { approveStatus } = res.data.accountInfo
					if (approveStatus == 2) {
						router.replace({ name: this.redirect });
					} else {
						router.replace('/doctornotice');
					}
				}
				cb && cb(res.data)
			}
		})
	}


	const getTagClassName = (val) => {
		switch (Number.parseInt(val)) {
			case 1: return 'text-success'
			case 2: return 'text-warning'
			case 3: return 'text-purple'
			default: return ''
		}
	}


	const getStoreGetters = (state, key, isJson = false) => {
		if (state[key]) return state[key]

		const sVal = sessionStorage.getItem(key)
		if (sVal) return !isJson ? sVal : JSON.parse(sVal)

		const lVal = localStorage.getItem(key)
		if (lVal) return !isJson ? lVal : JSON.parse(lVal)
	}
	
	// 讲者审核状态
	const speakerAuditStatus = (val) => {
		switch (Number.parseInt(val)) {
			// case -1: return '暂存';
			// case 0: return '待审核';
			// case 1: return '审核通过';
			// case 2: return '审核不通过';

			case 1: return '待审核';
			case 2: return '审核通过';
			case 3: return '审核不通过';
			case 4: return '暂存';
			default: return '--'
		}
	}

	// 讲者审核类型
	const speakerAuditStatusType = (val) => {
		switch (Number.parseInt(val)) {
			case 1: return 'warning';
			case 2: return 'success';
			case 3: return 'danger';
			default: return 'primary'
		}
	}

	// 会议审核状态
	const meetingAuditStatus = (val) => {
		switch (Number.parseInt(val)) {
			case -1: return '待召开';
			case 0: return '待审核';
			case 1: return '审核通过';
			case 2: return '审核拒绝';
			case 6: return '已取消';
			default: return '--'
		}
	}

  const getPrice = (val = 0) => {
    if (typeof val !== 'number') {
      console.error('类型错误，请传入number类型')
      return '0.00'
    }
    return val.toFixed(2)
  }

	return {
		getOpenid,
		loginByOpenid,
		getTagClassName,
		getStoreGetters,
		speakerAuditStatus,
    speakerAuditStatusType,
		meetingAuditStatus,
    getPrice
	}
}
